package com.liukai.personalbills.adapter;

import android.content.Context;
import android.widget.TextView;

import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.liukai.personalbills.R;
import com.liukai.personalbills.bean.BillsBean;
import com.liukai.personalbills.bean.BillsTitleBean;
import com.liukai.personalbills.util.TextUtil;
import com.liukai.personalbills.util.ToastUtil;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class BillsRecyclerAdapter extends GroupedRecyclerViewAdapter {

    private ArrayList<BillsTitleBean> billsTitleBeans;
    private Context context;

    public BillsRecyclerAdapter(Context context, boolean useBinding) {
        super(context, useBinding);
    }

    public BillsRecyclerAdapter(Context context, ArrayList<BillsTitleBean> titleBeanArrayList) {
        super(context);
        this.billsTitleBeans = titleBeanArrayList;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
//        return 10;
        return billsTitleBeans.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<BillsBean> billsBeans = billsTitleBeans.get(groupPosition).getBillsBeanArrayList();
        int size = billsBeans.size();

        return size;
//        return billsTitleBeans.get(groupPosition).getBillsBeanArrayList().size();
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.item_recyclerview_bills_year;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return 0;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_recyclerview_bills_day;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {

        TextView tvTitle = holder.get(R.id.tvDateYear);
        TextView tvIncome = holder.get(R.id.tvIncome);
        TextView tvExpend = holder.get(R.id.tvExpend);

        BillsTitleBean billsTitleBean = billsTitleBeans.get(groupPosition);

        tvTitle.setText(billsTitleBean.getDate());
        tvIncome.setText(billsTitleBean.getIncome());
        tvExpend.setText(billsTitleBean.getExpend());



    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {

    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {

        CircleImageView imgIcon = holder.get(R.id.imgIcon);
        TextView tvType = holder.get(R.id.tvName);
        TextView tvMoney = holder.get(R.id.tvMoney);
        TextView tvRemark = holder.get(R.id.tvRemark);

        BillsBean billsBean = billsTitleBeans.get(groupPosition).getBillsBeanArrayList().get(childPosition);

        imgIcon.setImageResource(billsBean.getImgIcon());
        tvType.setText(billsBean.getType()); // 消费类别
        tvMoney.setText(billsBean.getMoney()); // 金额
        if(billsBean.getRemark().equals("") || billsBean.getRemark() == null){
            tvRemark.setText("备注:" + "无"); // 备注
        }else{
            tvRemark.setText("备注:" + billsBean.getRemark()); // 备注
        }
        // 设置首页收入支出金额的字体颜色
        if(TextUtil.isIncome(billsBean.getType())){
            tvMoney.setTextColor(context.getColor(R.color.colorPrimaryDark));
        }else{
            tvMoney.setTextColor(context.getColor(R.color.colorAccent));
        }
        imgIcon.setCircleBackgroundColor(context.getColor(TextUtil.getNullColor()));
//        int color = billsBean.getTextColor();
//        ToastUtil.log(context,billsBean.getType(),color+"");

    }
}
